package com.ggh.jinjilive.ui.mall.community;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ggh.jinjilive.BaseApplication;
import com.ggh.jinjilive.R;
import com.ggh.jinjilive.databinding.FragmentCommunityBinding;
import com.ggh.jinjilive.mobile.mall.MallCommunityLIst;
import com.ggh.jinjilive.pagestatus.EmptyCallback;
import com.ggh.jinjilive.pagestatus.LoadingCallback;
import com.ggh.jinjilive.ui.BaseFragment;
import com.ggh.jinjilive.ui.mall.community.adapter.CommunityAdapter;
import com.ggh.jinjilive.ui.mall.goods.GoodsViewModel;
import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadSir;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class CommunityFragment extends BaseFragment<FragmentCommunityBinding> implements OnRefreshListener {
    private GoodsViewModel goodsViewModel;
    //适配器
    private CommunityAdapter communityAdapter;
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().getAppComponent().doInjectionMain(this);
        goodsViewModel = new ViewModelProvider(this,viewModelFactory).get(GoodsViewModel.class);
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable @org.jetbrains.annotations.Nullable ViewGroup container, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        return R.layout.fragment_community;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        View contentView = binding.getRoot().findViewById(R.id.refreshLayout);
        loadService = LoadSir.getDefault().register(contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadService.showCallback(LoadingCallback.class);
                goodsViewModel.communityList();
            }
        });
        loadService.showCallback(LoadingCallback.class);
        goodsViewModel.communityList();
    }

    @Override
    public void initView() {
        super.initView();
        binding.setMGoodsViewModel(goodsViewModel);

        binding.refreshLayout.setRefreshHeader(new MaterialHeader(mContext));
        binding.refreshLayout.setRefreshFooter(new ClassicsFooter(mContext));
        binding.refreshLayout.setEnableLoadMore(false);

        communityAdapter = new CommunityAdapter();
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        binding.recyclerView.setAdapter(communityAdapter);
    }

    @Override
    public void initData() {
        super.initData();
        //
        goodsViewModel.mMallCommunityLIst.observe(this, new Observer<List<MallCommunityLIst>>() {
            @Override
            public void onChanged(List<MallCommunityLIst> mallCommunityLIsts) {
                binding.refreshLayout.finishRefresh();
                if(null != mallCommunityLIsts && mallCommunityLIsts.size() > 0){
                    mallCommunityLIsts.add(0,null);
                    communityAdapter.setAdd(mallCommunityLIsts);
                    loadService.showSuccess();
                }else {
                    loadService.showCallback(EmptyCallback.class);
                }
            }
        });
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        //刷新
        binding.refreshLayout.setOnRefreshListener(this);
    }

    @Override
    public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
        goodsViewModel.communityList();
    }

}
